package com.PopulateDB;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.CMUBiS.util.Address;

public class MakeAddress {
	private static ArrayList<String> streets = MakeAddress.readStrings(FileLocations.streetsFile);
	private static ArrayList<String> towns = MakeAddress.readStrings(FileLocations.townsFile);
	private static StringBuffer strBuf = new StringBuffer();
	private static int streetsSize = streets.size();
	private static int townsSize = towns.size();	
	
	final static int minStreetWords = 1;
	final static int maxStreetWords = 3;
	final static int minTownWords = 1;
	final static int maxTownWords = 2;
	final static double fullZipFraction = 0.2;
	
	final static int streetDifference = maxStreetWords - minStreetWords;
	final static int townDifference = maxTownWords - minTownWords;
	
	
	public static Address getAddress(int userID, boolean isDefault){
		return new Address(-1, userID, getStreet(), getTown(), getZip(), PopulateStates.getRandomState(), isDefault);
	}
	
	//TODO: stringbuffer
	public static String getStreet(){
		int length = ((int)Math.round(Math.random() * streetDifference)) + minStreetWords;
		strBuf.setLength(0);
		
		if(length>0){
			strBuf.append((int)(Math.random()*1000));
			strBuf.append(" ");
			strBuf.append(streets.get(((int)Math.floor(Math.random() * streetsSize))));
		}
		
		for(int i=0; i<length-1; i++){
			strBuf.append(" ");
			strBuf.append(streets.get(((int)Math.floor(Math.random() * streetsSize))));
		}
		
		return strBuf.toString();
	}
	
	public static String getTown(){
		int length = ((int)Math.round(Math.random() * townDifference)) + minTownWords;
		strBuf.setLength(0);
		
		if(length>0)
			strBuf.append(towns.get(((int)Math.floor(Math.random() * townsSize))));
		
		for(int i=0; i<length-1; i++){
			strBuf.append(" ");
			strBuf.append(towns.get(((int)Math.floor(Math.random() * townsSize))));
		}
		
		return strBuf.toString();
	}
	
	public static String getZip(){
		strBuf.setLength(0);
		
		strBuf.append((int)(Math.random()*99999));
		
		if(Math.random() < fullZipFraction){
			strBuf.append("-");
			strBuf.append((int)(Math.random()*9999));
		}
		
		return strBuf.toString();
	}
	
	private static ArrayList<String> readStrings(String file){
		ArrayList<String> temp = new ArrayList<String>();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(MakeAddress.class.getResourceAsStream(file)));
			
			try {
				// read the street names
				while(in.ready()){
					String firstWord = in.readLine().split(",")[0];
					System.out.println(firstWord);
					temp.add(firstWord);
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		/*} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
			
			return temp;
	}
	
	public static void main(){
		MakeAddress ma = new MakeAddress();
		//ma.readStreets();
	}
}
